﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Consultant.Models
{
    public class UserRepository
    {
        private ProjectDataContext _datacontext;
        public UserRepository(ProjectDataContext datacontext)
        {
            _datacontext = datacontext;
        }
        /// <summary>
        /// Получение ключа по имени пользователя
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public Guid GetUserId(string UserName)
        {
            return _datacontext.aspnet_Users.SingleOrDefault(u => u.UserName == UserName).UserId;
        }

        /// <summary>
        /// Получение ключа роли по имени
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public Guid GetRoleId(string roleName)
        {
            return _datacontext.aspnet_Roles.SingleOrDefault(r => r.RoleName == roleName).RoleId;
        }

        public void CreateUserInRole(string userId, string roleId)
        {
            var g1 = new Guid(userId);
            var g2 = new Guid(roleId);
            var u = new aspnet_UsersInRole();
            u.UserId = g1;
            u.RoleId = g2;
            _datacontext.aspnet_UsersInRoles.InsertOnSubmit(u);
            _datacontext.SubmitChanges();
        }
    }
}